/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.edu.itschapala.sistemas.septimo.vista;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;
import mx.edu.itschapala.sistemas.septimo.control.ProductoLnLocal;
import mx.edu.itschapala.sistemas.septimo.modelo.Producto;
import mx.edu.itschapala.sistemas.septimo.vista.enumeracion.TipoBusqueda;

/**
 *
 * @author Instituto
 */
public class ProductoListar extends javax.swing.JFrame {
private final ProductoLnLocal productoControl;
private DefaultTableModel modelo;
private List<Producto> lista;
private JFrame ventana;

    /**
     * Creates new form ProductoListar
     */
    public ProductoListar(ProductoLnLocal productoControl) {
        this.productoControl=productoControl;
        initComponents();
        String nombres[]={"codigo","nombre","precio","departamento","descripcion"};
        //DefaultTableModel modelo = new DefaultTableModel(nombres, 0);
        
        modelo = new DefaultTableModel(nombres, 0){
public boolean isCellEdiatble(int row, int colum){

    return false;
}       
        };
         jTable1.setModel(modelo);
        
         lista= productoControl.getTodos();
        for(Producto p : lista){
            
            String renglon []= new String[6];
            renglon[0]=p.getCodigo()+"";
            renglon[1]=p.getNombre()+"";
            renglon[2]=p.getPrecio()+"";
            renglon[3]=p.getDepartamento()+"";
            renglon[4]=p.getDescripcion()+"";
            modelo.addRow(renglon);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jTextField1 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jRadioButton1.setText("Contiene");

        jRadioButton2.setText("Inicia");
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });

        jTextField1.setText("jTextField1");
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField1KeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(101, 101, 101)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 499, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(147, 147, 147)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jRadioButton2)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jRadioButton1)
                                .addGap(18, 18, 18)
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(110, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButton1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jRadioButton2)
                .addGap(20, 20, 20)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyPressed
        // TODO add your handling code here:
        List<Producto> tmp = new ArrayList<>();
        if(jRadioButton1.isSelected()){
            tmp =productoControl.getPorNombre(jTextField1.getText(), TipoBusqueda.CONTIENE);
        }else{
             tmp =productoControl.getPorNombre(jTextField1.getText(), TipoBusqueda.INICIA);
        }
          String nombres[]={"codigo","nombre","precio","departamento","descripcion"};
         modelo = new DefaultTableModel(nombres, 0){
public boolean isCellEdiatble(int row, int colum){

    return false;
}       
        };
        
        for(Producto p : productoControl.getTodos()){
            String renglon []= new String[5];
            renglon[0]=p.getCodigo()+"";
            renglon[1]=p.getNombre()+"";
            renglon[2]=p.getPrecio()+"";
            renglon[3]=p.getDepartamento()+"";
            renglon[4]=p.getDescripcion()+"";
            modelo.addRow(renglon);
      
        }
        jTable1.setModel(modelo);
        lista=tmp;
    }//GEN-LAST:event_jTextField1KeyPressed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
        if(evt.getClickCount()==2){
            System.out.println(jTable1.getSelectedRow());
            Producto p =lista.get(jTable1.getSelectedRow());
            
            if(ventana==null){
                ventana =new Dialogo(p, productoControl);
            }else{
                ventana.dispose();
                ventana=new Dialogo(p, productoControl);
            }
            ventana.setVisible(true);
        }
    }//GEN-LAST:event_jTable1MouseClicked

    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jRadioButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
